package gae.testjig.data;

import gae.testjig.dao.DaoException;
import gae.testjig.dao.DaoOrganization;
import gae.testjig.dto.Organization;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;


public class BizLogicOrganizations {

	public Organization createOrganization(String name) throws DaoException {
		PersistenceManager pm = PMF.getForTransactions().getPersistenceManager();
		try {
			pm.currentTransaction().begin();
			Organization org = pm.detachCopy( DaoOrganization.create(pm, name) );
			pm.currentTransaction().commit();
			return org;
		}
		finally {
			if (pm.currentTransaction().isActive()) {
		        pm.currentTransaction().rollback();
		    }
			pm.close();
		}
	}

	public void deleteOrganization(Key organizationKey) throws DaoException {
		PersistenceManager pm = PMF.getForTransactions().getPersistenceManager();
		try {
			pm.currentTransaction().begin();
			DaoOrganization.delete(pm, organizationKey);
			pm.currentTransaction().commit();
		}
		finally {
			if (pm.currentTransaction().isActive()) {
		        pm.currentTransaction().rollback();
		    }
			pm.close();
		}
	}

}
